/*
 * Copyright (C) 2009 Show SMS open source project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bydavy.util.showsms;

//~--- non-JDK imports --------------------------------------------------------

import android.content.Context;

import android.os.PowerManager;

/**
 * Class to manage the WakeLock
 * Used to wake up or lighting the phone screen
 *
 * @version        1.0, 09/07/02
 * @author         Davy L.
 */
public class WakeLockEasier {
    //~--- static fields ------------------------------------------------------

    /** WakeLock */
    private static PowerManager.WakeLock sWakeLock;

    //~--- methods ------------------------------------------------------------

    /**
     * Acquire the WakeLock
     * After this call, the phone will screen wake up and will bright until the WakeLock is released
     * If WakeLock was previously acquired, this have no effect
     * @param context
     */
    public static void acquireScreenWakeLockWithWakeUp(Context context) {
        Log.v("Acquiring wake lock");

        if (sWakeLock != null) {
            return;
        }

        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);

        sWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, Log.LOGTAG);
        sWakeLock.acquire();
    }

    /**
     * Acquire the WakeLock
     * After this call, the phone screen will bright until the WakeLock is released
     * If WakeLock was previously acquired, this have no effect
     * @param context
     */
    public static void acquireScreenWakeLock(Context context) {
        Log.v("Acquiring wake lock");

        if (sWakeLock != null) {
            return;
        }

        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);

        sWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, Log.LOGTAG);
        sWakeLock.acquire();
    }

    /**
     * Release the WakeLock
     * If no WakeLock was previously acquired, this function have no effect
     */
    public static void release() {
        Log.v("Releasing wake lock");

        if (sWakeLock != null) {
            sWakeLock.release();
            sWakeLock = null;
        }
    }
}
